/**
 * 线圈的实体
 */
cc.Class({
    extends: cc.Component,

    properties: {
        /**
         * 地图管理器
         */
        manager: {
            default: null,
            visible: false
        },
        /**
         * 旋转速度
         */
        rotateSpeed: {
            default: 0,
            visible: false
        }
    },

    onLoad () {
        this._renderer = this.node.getChildByName('Renderer');
        this._enemy = null;
        this._animation = this._renderer.getComponent(cc.Animation);
        this._isInner = false;
    },
    
    start () {
        
    },

    update (dt) {
        //圆盘旋转
        this._renderer.rotation += this.rotateSpeed * dt;
        //减少的量
        var speed = this.manager.getSpeed();
        var deltaRadius = dt * speed * 2;
        //自身的半径
        var selfWidth = this.manager._maxEdge * this.node.scaleX;
        var minusSelf = selfWidth - deltaRadius;

        //判读是否小于最小范围
        if (minusSelf < this.manager._minEdge) {
            this.resetState();
            minusSelf = this.manager._maxEdge - (this.manager._minEdge - minusSelf);
            this.node.opacity = 0;
            this.node.runAction(cc.fadeIn(this.manager.gap / (speed * 2)));
        }
        var scalePercent = minusSelf / this.manager._maxEdge;
        this.node.scaleX = this.node.scaleY = scalePercent;
    },

    /**
     * 设置自身的ID
     * @param {number} circleId ID
     */
    init (circleId) {
        this._id = circleId;
    },

    /**
     * 重置圆盘
     */
    resetState () {
        this._animation.stop();
        //设置到最底层
        this.manager.setCircleToBottom(this.node);
        this.manager.resetCircle(this);
    },

    /**
     * 将圆盘改成指定颜色
     * @param {cc.Color} color 改变的颜色
     */
    changeColor (color) {
        this._renderer.color = color;
    },

    /**
     * 给圆盘增加敌人
     * @param {cc.Node} enemy 敌人
     */
    addEnemy (enemy) {
        if (this._enemy != null) {
            this._renderer.removeChild(this._enemy);
            this._enemy.destroy();
        }
        this._renderer.addChild(enemy);
        this._enemy = enemy;
    },

    /**
     * 设置旋转速度
     * @param {number} speed 速度
     */
    changeRotateSpeed (speed) {
        this.rotateSpeed = speed;
    },

    /**
     * 反复横跳
     */
    jumpAround () {
        this._animation.play('jumpAround');
    },

    /**
     * 获取自身的旋转角
     * @returns 旋转角
     */
    getRotation () {
        return this._renderer.rotation;
    },

    /**
     * 往内移动一圈
     */
    setInnerCircle () {
        this.manager._isInner = true;
        var time = this.manager.scaleCircleTime;
        this.scheduleOnce(function(){this.manager._isInner = false}, time);
    },

    /**
     * 停止运动
     */
    stopAction () {
        this.rotateSpeed = 0;
        //反复横跳的敌人需要停止动画
        this._animation.stop();
    },
});
